//
//  DZYPageView.swift
//  DZYPageView
//
//  Created by dongzhenyu on 2017/7/6.
//  Copyright © 2017年 dongzhenyu. All rights reserved.
//

import UIKit

/**
 self 常见有两个地方不能省略
 1> 如果和上下文中其他标识符有歧义
 2> 闭包中用到当前对象的所有属性都不能省略
 
 */

class DZYPageView: UIView {

    // MARK: - 属性
    var titles: [String]
    var style: DZYPageStyle
    var childVcs: [UIViewController]
    var parentVc: UIViewController
    
    
    // MARK: - 构造函数
    // 在构造函数调用super.init()之前,必须保证所有的属性有被初始化
    init(frame: CGRect, titles: [String], style: DZYPageStyle, childVcs: [UIViewController], parentVc: UIViewController) {
        
        // 初始化
        self.titles = titles
        self.style = style
        self.childVcs = childVcs
        self.parentVc = parentVc
    
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
// MARK: - 设置UI界面
extension DZYPageView {
    fileprivate func setupUI() {
    
        // 1.创建DZYTitleView
        let titleFrame = CGRect(x: 0, y: 0, width: bounds.width, height: style.titleHeight)
        
        let titleView = DZYTitleView(frame: titleFrame, titles: titles, style: style)
        titleView.backgroundColor = UIColor.blue
        addSubview(titleView)
        
        // 2.创建DZYContentView
        let contentFrame = CGRect(x: 0, y: titleFrame.maxY, width: bounds.width, height: bounds.height - style.titleHeight)
        
        let contentView = DZYContentView(frame: contentFrame, childVcs: childVcs, parentVc: parentVc)
        contentView.backgroundColor = UIColor(hexString: "#888888")
        addSubview(contentView)
        // 3.让titleView和contentView进行沟通
        titleView.delegate = contentView
        contentView.delegate = titleView
    }
}

